package com.manopservice.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import com.manopservice.model.CarPart;
import com.manopservice.model.ResultPagination;
import com.manopservice.model.UploadFiles;
import com.manopservice.service.CarBrandService;
import com.manopservice.service.CarModelService;
import com.manopservice.service.CarPartService;
import com.manopservice.service.CarTypeService;
import com.manopservice.service.UploadFilesService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContextUtils;

@Controller
@RequestMapping("/carParts")
public class CarPartController {

	@Autowired
	private CarPartService carPartService;
	
	@Autowired
	private CarBrandService carBrandService;
	
	@Autowired
	private CarModelService carModelService;
	
	@Autowired
	private CarTypeService carTypeService;
	
	@Autowired
	private UploadFilesService uploadFilesService;
	
	
	@RequestMapping(value = "/showSelectModel", method = RequestMethod.POST)
	public ModelAndView getChild(@RequestParam int parent_id) {
			
		Map<String, Object> model = new HashMap<String, Object>();
		
		model.put("carModels", carModelService.findCarModelByCarBrand(carBrandService.findCarBrandById(parent_id)));
		model.put("modelId", carModelService.findCarModelById(parent_id));
		
			
		return new ModelAndView("showSelectModel", model);
	}
	
	@RequestMapping(value = "/ShowCarPart", method = RequestMethod.POST)
	public ModelAndView showCarPart(@RequestParam String text, @RequestParam  int pageSelect) {
		
		int paging = 12;
		
		Map<String, Object> model = new HashMap<String, Object>();
		
		ResultPagination<?> listCarPart = carPartService.findByModel(text, pageSelect, paging);		
		
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
	      String name = auth.getName(); //get logged in username
		//System.out.println("#####################  "+listCarPart.getCountResult()+" : "+listCarPart.getAmountPages());
		
	    if(!name.equals("anonymousUser")){
	    	model.put("username", name);
	    }
		model.put("carParts", listCarPart.getEntityClass());
		model.put("amountPages", listCarPart.getAmountPages());
		model.put("modelName", text);
		
		
		return new ModelAndView("ShowCarPart", model);
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ModelAndView updateCarPart(@ModelAttribute("carPart") CarPart  carPart, @RequestParam int carModelId, @RequestParam int carTypeId,
			BindingResult result) {
	
		carPart.setCarModel(carModelService.findCarModelById(carModelId));
		carPart.setCarType(carTypeService.findCarTypeById(carTypeId));
		carPartService.saveCarPart(carPart);
		return new ModelAndView("redirect:/carParts.html");
	}
	
	
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public ModelAndView insertCarPart(@ModelAttribute("carPart") CarPart  carPart, @RequestParam int carModelId, @RequestParam int carTypeId, UploadFiles uploadItem, HttpServletRequest request,
			BindingResult result) {	
		
		ServletContext context = RequestContextUtils.getWebApplicationContext(request).getServletContext();
		  String resource = "/resources/images/";
		  String fileName = "";
		  if(!result.hasErrors()){
			  fileName = uploadFilesService.uploadImages(uploadItem, context, resource);		  
		  }
		
		carPart.setCarModel(carModelService.findCarModelById(carModelId));
		carPart.setCarType(carTypeService.findCarTypeById(carTypeId));  
		carPart.setPicPath(fileName);
		carPartService.saveCarPart(carPart);
		return new ModelAndView("redirect:/carParts.html");
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public ModelAndView deletePart(@ModelAttribute("carPart") CarPart  carPart,
			BindingResult result) {
		carPartService.deleteCarPart(carPart.getId());
		return new ModelAndView("redirect:/carParts.html");
	}

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView listCarParts() {

		Map<String, Object> model = new HashMap<String, Object>();
		model.put("carParts",  carPartService.listCarParts());

		return new ModelAndView("carPartsList", model);
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public ModelAndView addCarPart(@ModelAttribute("carPart") CarPart carPart, @ModelAttribute("uploadFiles") UploadFiles upload,
			BindingResult result) {	
		
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("carBrands",  carBrandService.listCarBrands());		
		model.put("carModels", carModelService.listCarModels());		
		model.put("carTypes", carTypeService.listCarTypes());		
		
		return new ModelAndView("addCarPart", model);
	}
	
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public ModelAndView editCarPart(@ModelAttribute("carPart") CarPart carPart, @ModelAttribute("uploadFiles") UploadFiles upload,
			BindingResult result) {
		
		CarPart cp = carPartService.findById(carPart.getId());
		
		carPart.setName(cp.getName());
		carPart.setDetail(cp.getDetail());
		carPart.setCostPrice(cp.getCostPrice());
		carPart.setSalePrice(cp.getSalePrice());
		carPart.setDateStart(cp.getDateStart());
		carPart.setDateEnd(cp.getDateEnd());
		carPart.setStatus(cp.getStatus());
		carPart.setPicPath(cp.getPicPath());
		carPart.setCarModel(cp.getCarModel());
		carPart.setCarType(cp.getCarType());
		
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("carBrands",  carBrandService.listCarBrands());
		model.put("brandId",  carBrandService.findCarBrandById(cp.getCarModel().getCarBrand().getId()));
		model.put("carModels", carModelService.listCarModels());
		model.put("modelId", carModelService.findCarModelById(cp.getCarModel().getId()));
		model.put("carTypes", carTypeService.listCarTypes());
		model.put("typeId", carTypeService.findCarTypeById(cp.getCarType().getId()));
		
		return new ModelAndView("editCarPart", model);
	}

}